package com.cykj.service.impl;

import com.cykj.mapper.CombinationMapper;
import com.cykj.pojo.CyCombination;
import com.cykj.pojo.CyProject;
import com.cykj.service.CombinationService;
import com.cykj.util.PageResult;
import com.cykj.util.PageUtils;
import com.cykj.util.ResponseDto;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CombinationServiceImpl implements CombinationService {
    @Autowired
    private CombinationMapper combinationMapper;

    @Override
    public ResponseDto getAllCombination(int pageCurr, int pageSize) {
        Page<Object> page = PageHelper.startPage(pageCurr, pageSize);
        List<CyCombination> allCombination = combinationMapper.getAllCombination();
        if (allCombination != null) {
            PageInfo<Object> pageInfo = new PageInfo<>(page);
            PageResult pageResult = PageUtils.getPageResult(pageInfo);
            return new ResponseDto(1, "查询成功", pageResult);
        } else {
            return new ResponseDto(0, "查询失败", null);
        }
    }

    @Override
    public ResponseDto changeStatus(int proStatus, int CombinationId) {
        int i = combinationMapper.changeStatus(proStatus, CombinationId);
        if (i != 0) {
            return new ResponseDto(1, "修改成功", i);
        } else {
            return new ResponseDto(0, "修改失败", null);
        }
    }

    @Override
    public ResponseDto deleteRow(int combinationId) {
        int i = combinationMapper.deleteRow(combinationId);
        if (i != 0) {
            return new ResponseDto(1, "删除成功", null);
        } else {
            return new ResponseDto(0, "删除失败", null);
        }
    }

    @Override
    public ResponseDto lookInclude(int id) {
        List<CyProject> cyProjects = combinationMapper.lookInclude(id);
        return new ResponseDto(1, "成功", cyProjects);
    }

    @Override
    public ResponseDto getProjectName(int id) {
        List<CyProject> projectName = combinationMapper.getProjectName(id);
        String name = "";
        for (int i = 0; i < projectName.size(); i++) {
            if (i == projectName.size() - 1) {
                name = name + "《" + projectName.get(i).getProjectId() + ":" + projectName.get(i).getProjectName() + "》";
            } else {
                name = name + "《" + projectName.get(i).getProjectId() + ":" + projectName.get(i).getProjectName() + "》,";
            }
        }
        return new ResponseDto(1, "项目姓名", name);
    }

    @Override
    public ResponseDto alterInfo(int combinationId, String combinationName, String combinationPrice, String projectName) {
        //前端传过来的修改的项目是一个字符串，所有我们这边要来判断一下是否有修改项目
        //定义一个记录是否有修改项目值的
        int isAlterProject = 0;
        String msg = (String) getProjectName(combinationId).getData();
        if (!msg.equals(projectName)) {
            //不相等则说明有修改项目，那就来修改项

            //    获得id
            List<Integer> projectId = projectIsExist(projectName);
            if (projectId.size() == 1 && projectId.get(0) == 0 || projectId.get(0) == -1 ) {
                return new ResponseDto(-2, "项目修改信息填写有误", null);
            }
            //如果代码能执行到这里就说明要修改的填写没有错误
            //那我们就拿出id和name进行修改
            //那我们就是如果要修改的长度，可能增加，可能减少，那我们怎么办呢？？？ ----》删除记录重新添加
            //删除记录
            combinationMapper.deleteProjectRelative(combinationId);
            //添加记录，只需要id
            for (int j = 0; j < projectId.size(); j++) {
                //    循环插入记录
                //    获取集合里面的每个键值projectId
                combinationMapper.addProjectRelative(combinationId, projectId.get(j));
            }
            //到这里修改项目成功
            isAlterProject = 1;
            //然后就要修改其他的
        }
        //修改其他的
        CyCombination cyProject = combinationMapper.searchAlter(combinationId, combinationName, Integer.parseInt(combinationPrice));
        if (cyProject != null && isAlterProject == 0) {
            return new ResponseDto(2, "暂未修改", null);
        } else {
            int i = combinationMapper.alterInfo(combinationId, combinationName, Integer.parseInt(combinationPrice));
            if (i != 0) {
                return new ResponseDto(1, "修改成功", null);
            } else {
                return new ResponseDto(0, "修改失败", null);
            }
        }

    }

    //查询要添加或者是要修改的项目是否存在
    public List<Integer> projectIsExist(String projectName) {
        List<Integer> projectId = new ArrayList<>();
        String[] single = projectName.split(",");
        //    获得id和name
        for (int i = 0; i < single.length; i++) {
            if (!single[i].contains(":") || single[i].contains("：")) {
                projectId.clear();
                projectId.add(-1);//直接添加值为0，返回回去
                return projectId;
            }
            String[] split = single[i].split(":");//再通过：划分
            //去掉《》获得id和姓名
            int id = Integer.parseInt(split[0].replace("《", ""));
            String name = split[1].replace("》", "");
            if (!split[1].contains("》") || !split[0].contains("《")) {
                //如果不包含则说明填写格式有误
                projectId.clear();
                projectId.add(-1);//直接添加值为0，返回回去
                return projectId;
            }
            //查询所填写的id和name是否准确是否匹配
            //循环查询每一个要修改/添加的值是否一致
            int i1 = combinationMapper.searchProject(id, name);
            if (i1 == 0) {
                //如果有一个不存在则说明有一个填写错误，那么就可以直接return
                projectId.add(0);//直接添加值为0，返回回去
                return projectId;
            }
            projectId.add(id);//将id存入集合里面
        }
        return projectId;
    }

    @Override
    public ResponseDto searchInfo(String value, int pageCurr, int pageSize) {
        Page<Object> page = PageHelper.startPage(pageCurr, pageSize);
        List<CyCombination> cyCombination = combinationMapper.searchInfo(value);
        PageInfo<Object> pageInfo = new PageInfo<>(page);
        PageResult pageResult = PageUtils.getPageResult(pageInfo);
        return new ResponseDto(1, "查询成功", pageResult);
    }

    @Override
    public ResponseDto getAllName() {
        List<String> allName = combinationMapper.getAllName();
        return new ResponseDto(1, "查询成功", allName);
    }

    @Transactional(rollbackFor = Exception.class)//如果出现异常了直接回滚
    @Override
    public ResponseDto commitDelete(String checked) {
        //前端将要删除的细项名称变为字符串，再进行分隔
        String[] split = checked.split("，");
        int length = split.length;
        int count = 0;
        try {
            for (int i = 0; i < length; i++) {
                int res = combinationMapper.commitDelete(split[i]);
                if (res >= 1) {
                    count++;
                }
            }
            if (count == length) {
                return new ResponseDto(1, "删除成功", null);
            } else {
                return new ResponseDto(0, "删除失败", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    public ResponseDto addCommit(String name, String price, String includePro, String status) {
        //先判断要加的套餐的项目是否存在，若不存在则直接返回错误
        List<Integer> projectId = projectIsExist(includePro);
        if (projectId.size() == 1 && projectId.get(0) == 0) {
            return new ResponseDto(-2, "项目信息查找不到", null);
        } else if (projectId.size() == 1 && projectId.get(0) == -1) {
            return new ResponseDto(-1, "填写信息格式有误", null);
        }
        if (status.equals("2")) {
            status = "0";
        }
        int i = combinationMapper.addCommit(
                name, Integer.parseInt(price),
                Integer.parseInt(status));
        //到这说明combination表已经插入，可以查找对应的id
        int combinationId = combinationMapper.selectNewCombinationId(name, Integer.parseInt(price));
        //添加记录，只需要id
        for (int j = 0; j < projectId.size(); j++) {
            //    循环插入记录
            //    获取集合里面的每个键值projectId
            combinationMapper.addProjectRelative(combinationId, projectId.get(j));
        }
        return new ResponseDto(1, "项目插入成功", i);
    }

    @Override
    public ResponseDto borderPriceSearch(int minPrice, int maxPrice, int pageCurr, int pageSize) {
        Page<Object> page = PageHelper.startPage(pageCurr, pageSize);
        List<CyProject> cyProjects = combinationMapper.borderPriceSearch(minPrice, maxPrice);
        if (cyProjects == null){
            return new ResponseDto(0,"查找信息为空",null);
        }else {
            PageInfo<Object> pageInfo = new PageInfo<>(page);
            PageResult pageResult = PageUtils.getPageResult(pageInfo);
            return new ResponseDto(1,"价格区间查找成功",pageResult);
        }
    }

}
